﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using Models;
using System.Collections.Generic;

namespace PCCS.Management.AdminManage
{
    public partial class AddAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {

                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }

            DisplayLeftTypeInfo();
        }

        protected void DisplayLeftTypeInfo()
        {
            IList<LeftType> types = LeftTypeManager.GetLeftTypesParent();
            foreach (LeftType type in types)
            {
                string nodeId = type.LeftTypeID.ToString();//ID
                string displayName = type.TypeTitle;//Name
                //加载用户控件
                UserControl roleContol = (UserControl)LoadControl(@"RoleUserControl.ascx");
                //从用户控件中查找名为"hidParentMenu",并将value设为菜单ID 
                HtmlInputHidden hidParentMenu = (HtmlInputHidden)roleContol.FindControl("hidParentMenu");
                hidParentMenu.Value = nodeId;
                //从用户控件中查找"chkParentMenu",将Text属性设为菜单名称
                CheckBox chkParentMenu = (CheckBox)roleContol.FindControl("chkParentMenu");
                chkParentMenu.Text = displayName;
                //最后将其添加到控件容器中
                phRoleDistribute.Controls.Add(roleContol);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string adminName = txtAdminName.Text.Trim();
            string passWord = txtPassword.Text.Trim();

            int count = AdminManager.GetAdminByName(adminName).Count;
            if (count > 0)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('已存在该管理员');", true);
            }
            else
            {
                Admin admin = new Admin();
                admin.AdminName = adminName;
                admin.PassWord = passWord;
                admin.AdminDesc = txtAdminDesc.Text.Trim();
                AdminManager.AddAdmin(admin);

                Admin top1 = AdminManager.GetAdminBytop1();
                int adminID = top1.AdminID;

                foreach (Control ct in phRoleDistribute.Controls)
                {
                    CheckBox chk = (CheckBox)ct.FindControl("chkParentMenu");
                    HtmlInputHidden hih = (HtmlInputHidden)ct.FindControl("hidParentMenu");

                    if (chk.Checked)
                    {
                        LeftLinkManager.AddLeftLink(adminID, Convert.ToInt32(hih.Value), 0);
                    }


                    CheckBoxList chklst = (CheckBoxList)ct.FindControl("chkChildMenu");
                    foreach (ListItem li in chklst.Items)
                    {
                        if (li.Selected)
                        {
                            LeftLinkManager.AddLeftLink(adminID, Convert.ToInt32(li.Value), Convert.ToInt32(hih.Value));
                        }
                    }
                }

                Models.AdminLog log = new Models.AdminLog();
                int logId = Convert.ToInt32(Session["admin"].ToString());
                string logName = AdminManager.GetAdminByAdminID(logId).AdminName;
                log.AdminName = logName;
                log.LogTime = DateTime.Now;
                log.LogDesc = "成功添加管理员" + adminName + "。";
                AdminLogManager.AddAdminLog(log);

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('成功添加管理员');", true);
            }
        }
    }
}
